package com.zbkj.admin.controller.platform;

import com.github.pagehelper.PageInfo;
import com.zbkj.dal.base.page.CommonPage;
import com.zbkj.dal.pojo.request.PageParamRequest;
import com.zbkj.dal.pojo.request.ba.ConcludeDiscoveryRequest;
import com.zbkj.dal.pojo.request.ba.QueryDiscoveryRequest;
import com.zbkj.dal.pojo.response.ba.DiscoveryDetailResponse;
import com.zbkj.dal.pojo.response.ba.DiscoveryResponse;
import com.zbkj.dal.base.result.CommonResult;
import com.zbkj.service.service.DiscoveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @author tianzhonghai
 * 2023/10/9 08:58
 */
@Slf4j
@RestController
@RequestMapping("api/admin/platform/discovery")
@Api(tags = "全球找货")
public class DiscoveryController {
    @Autowired
    private DiscoveryService discoveryService;

    @ApiOperation(value = "分页获取全球找货信息")
    @RequestMapping(value = "/pageDiscovery", method = RequestMethod.POST)
    public CommonResult<CommonPage<DiscoveryResponse>> pageDiscovery(@RequestBody QueryDiscoveryRequest queryDiscoveryRequest, PageParamRequest pageParamRequest) {
        if (CollectionUtils.isNotEmpty(queryDiscoveryRequest.getTimes())) {
            queryDiscoveryRequest.setBeginDate(queryDiscoveryRequest.getTimes().get(0));
            queryDiscoveryRequest.setEndDate(queryDiscoveryRequest.getTimes().get(1));
        }
        PageInfo<DiscoveryResponse> sharePageInfo = discoveryService.queryDiscovery(queryDiscoveryRequest, pageParamRequest);
        return CommonResult.success(CommonPage.restPage(sharePageInfo));
    }

    @ApiOperation(value = "全球找货详情")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public CommonResult<DiscoveryDetailResponse> detail(@PathVariable("id")Integer id) {
        return CommonResult.success(discoveryService.detail(id));
    }

    @ApiOperation(value = "确认全球找货信息")
    @RequestMapping(value = "/confirm", method = RequestMethod.POST)
    public CommonResult<Boolean> confirm(@Validated @RequestBody ConcludeDiscoveryRequest concludeDiscoveryRequest) {
        if (concludeDiscoveryRequest.getServiceFee() == null) {
            concludeDiscoveryRequest.setServiceFee(BigDecimal.ZERO);
        }
        return CommonResult.success(discoveryService.confirm(concludeDiscoveryRequest));
    }
}
